const mongoUtil = require("./mongo_util")
const collectionName = "categories"
const collectionsearchName = "indexInfo"

function getCategories(callback) {
    const mmp = mongoUtil.getCollection(collectionName)
    mmp.then(collection => {
        collection.find().toArray(function (err, result) {
            // console.log("resultjjjj",result);
            if (result.length) {
                callback(null, result)
                new Error()
            } else {
                callback(err ? err : {
                    reason: "获取失败"
                }, result)
            }
        })
    })
}


// db.userInfo.find({name: /mongo/});

function getGoods(callback) {
    const mmp = mongoUtil.getCollection(collectionsearchName)
    mmp.then(collection => {
        collection.find().toArray(function (err, result) {
            if (result.length) {
                callback(null, result)
                new Error()
            } else {
                callback(err ? err : {
                    reason: "获取失败"
                }, result)
            }
        })
    })
}



function addCategories(Categories, callback) {
    const whereStr = {
        'CategoriesId': "6184e581603caaa12a95b2d8",
        "categories.data.categoryId": {
            $ne: Categories.categoryId
        }
    }
    const updateStr = {
        $push: {
            'categories.data': Categories
        }
    };
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.updateOne(whereStr, updateStr, function (err, res) {

            callback(err, res)
        });
    })
}

function updateCategories(putCategories, callback) {
    var whereStr = {
        // 'indexId': "61820c137b26cb14db4eedfa",
        'categories.data.categoryId': putCategories.categoryId,
    }
    var updateStr = {
        $set: {
            'categories.data.$': putCategories
        }
    };
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.updateOne(whereStr, updateStr, function (err, res) {
            callback(err, res)
        });
    })
}


function deleteCategories(categoryId, callback) {
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.find({}).toArray(function (err, result) {
            result.forEach(item => {
                const Categorieslist = item.categories.data.filter(ite => {
                    return !categoryId.includes(ite.categoryId)
                })
                var whereStr = {
                    'CategoriesId': item.CategoriesId,
                }
                var updateStr = {
                    $set: {
                        'categories.data': Categorieslist
                    }
                };
                mongoUtil.getCollection(collectionName).then(collection => {
                    collection.updateOne(whereStr, updateStr, function (err, res) {
                        callback(err, res)
                    });
                })

            })
        })
    })
}

// 添加二级标题
function addsecondCategories(secondCategories, callback) {
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.find({}).toArray(function (err, result) {
            // console.log("====",result);
            result.forEach(item => {
                item.categories.data.forEach(ite => {
                    if (ite.categoryId == secondCategories.parentId) {
                        // console.log("////", ite);
                        if (ite.secondLevelCategoryVOS) {
                            console.log(1111);
                            ite.secondLevelCategoryVOS.unshift(secondCategories)
                        } else {
                            ite['secondLevelCategoryVOS'] = [secondCategories]
                        }

                    }
                })
                // console.log(item.categories.data);
                var whereStr = {
                    'CategoriesId': item.CategoriesId,
                }
                var updateStr = {
                    $set: {
                        'categories.data': item.categories.data
                    }
                };
                mongoUtil.getCollection(collectionName).then(collection => {
                    collection.updateOne(whereStr, updateStr, function (err, res) {
                        callback(err, res)
                    });
                })

            })
        })
    })
}

function updatesecondCategories(putsecondCategories, callback) {
    console.log("putsecondCategories", putsecondCategories.categoryId);
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.find({}).toArray(function (err, result) {
            // console.log("====",result);
            result.forEach(item => {
                item.categories.data.forEach(ite => {
                    if (ite.categoryId == putsecondCategories.parentId) {
                        // console.log("////", ite);
                        ite.secondLevelCategoryVOS.forEach((pre, index) => {
                            if (pre.categoryId == putsecondCategories.categoryId) {
                                // console.log(pre,index);
                                ite.secondLevelCategoryVOS[index] = putsecondCategories
                            }
                        })
                        console.log(ite.secondLevelCategoryVOS);
                    }
                })
                // console.log("===",item.categories.data);
                var whereStr = {
                    'CategoriesId': item.CategoriesId,
                }
                var updateStr = {
                    $set: {
                        'categories.data': item.categories.data
                    }
                };
                mongoUtil.getCollection(collectionName).then(collection => {
                    collection.updateOne(whereStr, updateStr, function (err, res) {
                        callback(err, res)
                    });
                })

            })
        })
    })
}


function deletesecondCategories(secondCategoriesId, callback) {
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.find({}).toArray(function (err, result) {
            result.forEach(item => {
                // console.log(item.CategoriesId);
                item.categories.data.forEach(pre => {

                    const secondLevelCategoryVOS = pre.secondLevelCategoryVOS.filter(ite => {

                        return !secondCategoriesId.includes(ite.categoryId)
                    })

                    pre.secondLevelCategoryVOS = secondLevelCategoryVOS
                })
                var whereStr = {
                    'CategoriesId': item.CategoriesId,
                }
                var updateStr = {
                    $set: {
                        'categories.data': item.categories.data
                    }
                };
                mongoUtil.getCollection(collectionName).then(collection => {
                    collection.updateOne(whereStr, updateStr, function (err, res) {
                        callback(err, res)
                    });
                })
            })
        })
    })
}
module.exports = {
    getCategories,
    getGoods,
    addCategories,
    updateCategories,
    deleteCategories,
    addsecondCategories,
    updatesecondCategories,
    deletesecondCategories

}